<?php
/**
*
* @author Blackpawn blackpawn666@hotmail.com - http://blackpawnsarchives.net
* @co_author GameCharmer admin@gamecharmer.com - http://gaming.gamecharmer.com
* @package Age of Dragons Mod :: ( Game Console Required )
* @version $Id: index.php 1.1.1 12-26-2009 Blackpawn
* @copyright (c) 2009 Blackpawns Archives
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/

define('GC_MOD', true);
define('IN_PHPBB', true);
define('AOD', true);

$phpbb_root_path =  '../../';
$templates_path = '../../../games/aod/templates/';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include($phpbb_root_path . 'includes/mods/game_console/gc_common.' . $phpEx);
include($phpbb_root_path . 'games/aod/common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();

// Set custom language path
$user->set_custom_lang_path($phpbb_root_path.'games/aod/language/');

//load our language file
$user->add_lang('common');

// Now add the phpBB language path
$user->set_custom_lang_path($phpbb_root_path . 'language');

// Load the Game Console language file
$user->add_lang('mods/game_console');

if ($user->data['user_id'] == ANONYMOUS)
{
	login_box('', $user->lang['LOGIN']);
}

if ($user->data['is_bot'])
{
	redirect(append_sid("{$phpbb_root_path}index.$phpEx"));
}

// Default Vars
$news_page = TRUE; // Highlights the news tab
$error_msgs = null;
$admin = 0;

// Var Requests
$cmd = request_var('cmd','');
$create = request_var('create','');

// AOD Config


// Gather some user info
$user_id = $user->data['user_id'];

// Fetch kingdom data
$king = fetch_kingdom_data($user_id);
$admin_access = $king['admin_access'];
$account_status = gc_check_account_status($user_id,$game_id);
$kingdom_status = $king['kingdom_status'];

// Check to see if this user is a admin staff member
if ($user_id == 2 || $admin_access == 1)
{
	$admin = 1;
}

// Check kingdom status to see if this account is frozen
if($kingdom_status == 1)
{
	// Lets make sure the main admin can't be blocked
	if ($user_id <> 2){
		trigger_error($user->lang['FROZEN'] . back_link());
	}
}

// Make sure the user really wants to delete their account
if ($cmd == 'delete_account')
{		
		
	if(!$account_status)
	{
		trigger_error($user->lang['NO_GAME_ACCOUNT_FOUND'] . back_link());
	}
	
	if(!$game_id)
	{
		trigger_error($user->lang['MISSING_GAME_ID'] . back_link());
	}

	if($game_id)
	{
		$s_hidden_fields = build_hidden_fields(array(
			'cmd' => 'delete_confirmed',
			)
		);

		//Display confirm box
		confirm_box(false, $user->lang['CONFIRM_GAME_DELETE'], $s_hidden_fields);	
	}
}

// Delete the users account
if ($cmd == 'delete_confirmed')
{

	if (confirm_box(true))				
	{		
		// Remove the selected game
		delete_account($game_id,$user_id);
	
		add_log('user', $user_id,'AOD_USER_LOG_DELETE_ACCOUNT','');
		trigger_error($user->lang['ACCOUNT_DELETED_SUCCESSFULLY'] . return_to_gamelist_link());
	}else{
		redirect(append_sid("{$phpbb_root_path}games/index.$phpEx"));
	}
	
}


// If user doesn't have an account then set them up with one
if(!$account_status)
{

	if($cmd == $user->lang['CREATE'])
	{
		$kingdom_ruler = request_var('kingdom_ruler', '');
		if(empty($kingdom_ruler))
		{
			$kingdom_ruler = $user->data['username'];
		}
		
		$kingdom_name = request_var('kingdom_name', '');
		$kingdom_advantage	= request_var('kingdom_advantage', 0);
		
		create_account($game_id,$user_id,$kingdom_ruler,$kingdom_name,$kingdom_advantage);
	}
	
	include($phpbb_root_path.'games/aod/create.php');
	
	exit;
}

// News
$sql = 'SELECT * FROM ' . AOD_NEWS_TABLE . ' ORDER BY id DESC LIMIT 25';
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
	$news_post = $row['news'];
	$news_post = str_replace("\n", "<br />", $news_post);
	
	$template->assign_block_vars('newsrow', array(
		'NEWS'		=> $news_post,
		'DATE'		=> $row['date'],
		'USER'		=> gc_fetch_users_name($row['user_id']),
	));
}
$db->sql_freeresult($result);


// Top Ten Players
$sql = 'SELECT * FROM ' . AOD_KINGDOMS_TABLE . ' ORDER BY kingdom_net DESC';
$result = $db->sql_query($sql);
$i = 1;
while ($row = $db->sql_fetchrow($result))
{

	$template->assign_block_vars('playersrow', array(
		'RANK'			=> $i,
		'KINGDOM'		=> $row['kingdom_name'],
		'LEVEL'			=> number_format($row['kingdom_level']),
		'LAND'			=> number_format($row['kingdom_total_land']),
		'POPULATION'	=> number_format($row['kingdom_pop']),
		'NETWORTH'		=> number_format($row['kingdom_net']),
	));
	++$i;
}
$db->sql_freeresult($result);

// Display an error messages to the user
if($error_msgs){
	$error_msgs = '<span class="errormsg"><b>'.$error_msgs.'</b></span>';
}

include($phpbb_root_path . 'games/aod/includes/page_header.' . $phpEx);

// Output page
page_header($game_name.' - '.$user->lang['NEWS']);

$template->set_filenames(array(
	'body' => $templates_path.'index_body.html')
);

page_footer();			

?>